package xhwl.hmo.mall.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import xhwl.hmo.mall.BaseActivity;
import xhwl.hmo.mall.Constant;
import xhwl.hmo.mall.IntentConstants;
import xhwl.hmo.mall.R;
import xhwl.hmo.mall.adapter.ImageBucketAdapter;
import xhwl.hmo.mall.bean.ImageBucket;
import xhwl.hmo.mall.utils.ImageFetcher;


/**
 * 选择相册
 * 
 */

public class ImageBucketChooseActivity extends BaseActivity {
	private ImageFetcher mHelper;
	private List<ImageBucket> mDataList = new ArrayList<ImageBucket>();
	private ListView mListView;
	private ImageBucketAdapter mAdapter;
	private int availableSize;
	public static String EXTRA_FROM_TYPE="fromType";
	int fromType=0;//1-发送图片进来的
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		fromType=getIntent().getIntExtra(EXTRA_FROM_TYPE, 0);
		availableSize = getIntent().getIntExtra(
				IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE,
				Constant.MAX_IMAGE_SIZE);
		mHelper = ImageFetcher.getInstance(getApplicationContext());
		setContentView(R.layout.act_image_bucket_choose);
	}

	@Override
	public void initViews() {
		initTitleViews();
		mListView = (ListView) findViewById(R.id.listview);

		mListView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
									int position, long id) {

				selectOne(position);

				Intent intent = new Intent(getBaseContext(),
						ImageChooseActivity.class);
				intent.putExtra(ImageBucketChooseActivity.EXTRA_FROM_TYPE, fromType);
				intent.putExtra(IntentConstants.EXTRA_IMAGE_LIST,
						(Serializable) mDataList.get(position).imageList);
				intent.putExtra(IntentConstants.EXTRA_BUCKET_NAME,
						mDataList.get(position).bucketName);
				intent.putExtra(IntentConstants.EXTRA_CAN_ADD_IMAGE_SIZE,
						availableSize);

				startActivity(intent);
				finish();
			}
		});
	}

	@Override
	protected void setUpTitleView() {
		super.setUpTitleView();
		titleTextView.setText("相册");
//		rightTextView.setText("取消");
	}

	@Override
	public void initListener() {

	}

	@Override
	public void loadData() {
		mDataList = mHelper.getImagesBucketList(false);
		mAdapter = new ImageBucketAdapter(this, mDataList);
		mListView.setAdapter(mAdapter);
	}
	private void selectOne(int position) {
		int size = mDataList.size();
		for (int i = 0; i != size; i++) {
			if (i == position)
				mDataList.get(i).selected = true;
			else {
				mDataList.get(i).selected = false;
			}
		}
		mAdapter.notifyDataSetChanged();
	}

}
